package com.qs.dao;


import java.util.List;

import org.hibernate.LockOptions;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Example;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.qs.entities.UserSession;


public class UserSessionDAO extends BaseHibernateDAO {
	
	private static final Logger log = LoggerFactory.getLogger(UserSessionDAO.class);
	
	public static final String SESSION_ID = "sessionId";
	
	
	public void save(com.qs.entities.UserSession transientInstance) {
		log.debug("saving Session instance");
		Session session = null;
    	Transaction tx = null;
		try {
			session = getSession();
			tx 		= session.beginTransaction();
			session.save(transientInstance);
			tx.commit();
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			try{
				tx.rollback();
			}
			catch(RuntimeException rbe){
				log.error("Couldn't roll back transaction", rbe);
			}
			throw re;
		}
		finally{
			if(session!=null)
				session.close();
		}
	}

	public void delete(Session persistentInstance) {
		log.debug("deleting Session instance");
		try {
			getSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public UserSession findById(java.lang.String id) {
		log.debug("getting Session instance with id: " + id);
		try {
			String queryString = "from UserSession as model where model."+ SESSION_ID + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, id);
			return (UserSession)queryObject.list().get(0);
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(Session instance) {
		log.debug("finding Session instance by example");
		try {
			List results = getSession().createCriteria("Session")
					.add(Example.create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Session instance with property: "
				+ propertyName + ", value: " + value);
		try {
			String queryString = "from Session as model where model."
					+ propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	
	public List findAll() {
		log.debug("finding all Session instances");
		try {
			String queryString = "from Session";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public com.qs.entities.UserSession merge(com.qs.entities.UserSession detachedInstance) {
		log.debug("merging Session instance");
		try {
			com.qs.entities.UserSession result = (com.qs.entities.UserSession) getSession().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}
	
	public void updateUserSessionByProperty(String session_id,String property,boolean isExpire){
		Session session = null;
    	Transaction tx = null;
		try {
			session = getSession();
			tx 		= session.beginTransaction();
			Query query = session.createQuery("update UserSession set "+property+" =:isExpire where sessionId=:sessionId");
			query.setParameter("isExpire", isExpire);
			query.setParameter("sessionId", session_id);
 			query.executeUpdate();
			tx.commit();
			log.debug("update successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			try{
				tx.rollback();
			}
			catch(RuntimeException rbe){
				log.error("Couldn't roll back transaction", rbe);
			}
			throw re;
		}
		finally{
			if(session!=null)
				session.close();
		}
	}
	
	public void updateUserSessionByExpiry(String session_id,boolean isExpiry){
		updateUserSessionByProperty(session_id,"isExpired",isExpiry);
	}

	public void attachDirty(com.qs.entities.UserSession instance) {
		log.debug("attaching dirty Session instance");
		Session session = null;
    	Transaction tx = null;
		try {
			session = getSession();
			tx 		= session.beginTransaction();
			session.saveOrUpdate(instance);
			tx.commit();
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			try{
				tx.rollback();
			}
			catch(RuntimeException rbe){
				log.error("Couldn't roll back transaction", rbe);
			}
			throw re;
		}
		finally{
			if(session!=null)
				session.close();
		}
	}

	public void attachClean(com.qs.entities.UserSession instance) {
		log.debug("attaching clean Session instance");
		try {
			getSession().buildLockRequest(LockOptions.NONE).lock(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}
}